package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.rdb.api.TargetingSpecTemplateRdbService;
import com.ndp.fb.rdb.model.TargetingSpecTemplate;
import com.ndp.fb.walle.business.TargetingSpecTemplateService;
import com.ndp.fb.walle.model.vo.targetingspectemplate.param.TargetingSpecTemplateParam;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/3/25.
 */
@Service
public class TargetingSpecTemplateServiceImpl implements TargetingSpecTemplateService {

    @Reference
    private TargetingSpecTemplateRdbService targetingSpecTemplateRdbService;

    @Override
    public List<TargetingSpecTemplate> findAllTargetingSpecTemplates(Long orgId, Long fbConnectionObjectId, Long productConfigurationId) throws BusinessException {
        List<TargetingSpecTemplate> targetingSpecTemplateList
                = targetingSpecTemplateRdbService.findByOrgIdAndFbConnectObjectIdAndProductConfigurationId(
                orgId, fbConnectionObjectId, productConfigurationId);
        return targetingSpecTemplateList;
    }

    @Override
    public Long createTargetingSpecTemplate(TargetingSpecTemplateParam targetingSpecTemplateParam) throws BusinessException {
        checkNameIsDuplicate(targetingSpecTemplateParam);
        TargetingSpecTemplate targetingSpecTemplate = new TargetingSpecTemplate();
        targetingSpecTemplate.setSpec(targetingSpecTemplateParam.getSpec());
        targetingSpecTemplate.setDescription(targetingSpecTemplateParam.getDescription());
        targetingSpecTemplate.setName(targetingSpecTemplateParam.getName());
        targetingSpecTemplate.setType(targetingSpecTemplateParam.getType().getIndex());
        targetingSpecTemplate.setUserId(targetingSpecTemplateParam.getUserId());
        targetingSpecTemplate.setOrgId(targetingSpecTemplateParam.getOrgId());
        targetingSpecTemplate.setFbConnectionObjectId(targetingSpecTemplateParam.getFbConnectObjectId());
        targetingSpecTemplate.setProductConfigurationId(targetingSpecTemplateParam.getProductConfigurationId());
        Date date = new Date();
        targetingSpecTemplate.setCreateTime(date);
        targetingSpecTemplate.setCreateUtcTime(date.getTime());
        return targetingSpecTemplateRdbService.save(targetingSpecTemplate);
    }



    /**
     * 校验名称是否重复
     * @throws com.ndp.fb.exception.BusinessException BusinessException
     */
    private void checkNameIsDuplicate(TargetingSpecTemplateParam targetingSpecTemplateParam) throws BusinessException {
        Long count = targetingSpecTemplateRdbService.countByOrgIdFbConnectionObjectIdProductConfigurationIdAndName(
                targetingSpecTemplateParam.getOrgId(), targetingSpecTemplateParam.getFbConnectObjectId(),
                targetingSpecTemplateParam.getProductConfigurationId(), targetingSpecTemplateParam.getName());
        if (null != count && 0 < count) {
            throw new BusinessException(ErrorType.TARGETINGSPECTEMPLATE_NAME_EXISTS);
        }
    }
}
